package it.polimi.traveldream.ejb.dtocreator;

import it.polimi.traveldream.ejb.entities.Excursion;
import it.polimi.traveldream.ejb.entities.Hotel;
import it.polimi.traveldream.ejb.entities.Transport;
import it.polimi.traveldream.ejb.entities.Travelpackage;
import it.polimi.traveldream.ejbclient.dtos.ExcursionDTO;
import it.polimi.traveldream.ejbclient.dtos.HotelDTO;
import it.polimi.traveldream.ejbclient.dtos.TransportDTO;
import it.polimi.traveldream.ejbclient.dtos.TravelPackageDTO;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class TravelPackageDTOCreator {
	@SuppressWarnings("unchecked")
	public static TravelPackageDTO createTravelPackage(Travelpackage entity) {
		BigDecimal totalPriceHotels = computeTotalPriceHotels(entity.getHotels());
		BigDecimal totalPriceTransports = computeTotalPriceTransports(entity.getTransports());
		BigDecimal totalPriceExcursions = computeTotalPriceExcursions(entity.getExcursions());
		return entity != null ? new TravelPackageDTO(entity.getId(), entity.getBasePrice(),
				totalPriceHotels.add(totalPriceTransports).add(totalPriceExcursions).add(entity.getBasePrice()),
				entity.getDescription(), entity.getEndTrip(), entity.getExpiry(), entity.getFeasibleReservations(),
				entity.getOverpast(), entity.getStartTrip(), entity.getType(),
				(List<ExcursionDTO>) BaseProductDTOCreator.createBaseProductList(entity.getExcursions()),
				(List<HotelDTO>) BaseProductDTOCreator.createBaseProductList(entity.getHotels()),
				(List<TransportDTO>) BaseProductDTOCreator.createBaseProductList(entity.getTransports()))
		: null;
	}
	
	private static BigDecimal computeTotalPriceHotels(List<Hotel> hotels) {
		BigDecimal price = new BigDecimal(0);
		for(Hotel h : hotels) {
			price = price.add(h.getPrice());
		}
		return price;
	}

	private static BigDecimal computeTotalPriceTransports(List<Transport> transports) {
		BigDecimal price = new BigDecimal(0);
		for(Transport t : transports) {
			price = price.add(t.getPrice());
		}
		return price;
	}

	private static BigDecimal computeTotalPriceExcursions(List<Excursion> excursions) {
		BigDecimal price = new BigDecimal(0);
		for(Excursion e : excursions) {
			price = price.add(e.getPrice());
		}
		return price;
	}

	public static List<TravelPackageDTO> createTravelPackageList(Collection<Travelpackage> entities) {
		if(entities != null && !entities.isEmpty()) {
			List<TravelPackageDTO> travelPackages = new ArrayList<TravelPackageDTO>();
			for(Travelpackage entity : entities) {
				travelPackages.add(createTravelPackage(entity));
			}
			return travelPackages;
		}
		return null;
	}
	
}
